package io.leo.leoui.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import io.leo.leoui.R;
import io.leo.leoutils.StringUtils;

public class RoundDialog extends Dialog {
    public static final int BTN1 = R.id.dr_btn1;
    public static final int BTN2 = R.id.dr_btn2;


    private TextView textView;
    private Button btn1,btn2;
    private LinearLayout viewGroup;
    private View view;

    private String title;
    private String btn1Text;
    private String btn2Text;
    private Context context;
    private View.OnClickListener btn1Listener;
    private View.OnClickListener btn2Listener;
    private boolean matchParent = false;
    public RoundDialog( Context context) {
        this(context,R.style.round_dialog);
    }

    public RoundDialog( Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
    }

    public void setView(View view) {
        this.view = view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_round);
        textView = findViewById(R.id.dr_title);
        viewGroup = findViewById(R.id.dr_viewgroup);
        btn1 = findViewById(R.id.dr_btn1);
        btn2 = findViewById(R.id.dr_btn2);

        if (!StringUtils.isEmpty(title))
            textView.setText(title);
        if (!StringUtils.isEmpty(btn1Text)){
            btn1.setText(btn1Text);
            btn1.setOnClickListener(btn1Listener);
            btn1.setVisibility(View.VISIBLE);
        }
        if (!StringUtils.isEmpty(btn2Text)){
            btn2.setText(btn2Text);
            btn2.setOnClickListener(btn2Listener);
            btn2.setVisibility(View.VISIBLE);
        }

        if (view!=null){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            viewGroup.addView(view);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        this.title = title.toString();
    }

    public void setBtn1(String btnText,View.OnClickListener listener) {
        this.btn1Text = btnText;
        this.btn1Listener=listener;
    }

    public void setBtn2(String btnText,View.OnClickListener listener){
        this.btn2Text = btnText;
        this.btn2Listener = listener;
    }

    public void matchParent(Activity activity){
        this.matchParent = true;
        this.context = activity;
    }

    @Override
    public void show() {
        super.show();
        if (matchParent){
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.width = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth()-100;
            getWindow().setAttributes(params);
        }
    }
}
